var currentUTC = new Date();
var Jsonp = /** @class */ (function () {
    function Jsonp() {
    }
    /**
     * 发送jsonp请求
     * @param {string} url
     * @param {string} callbackName
     * @param {object} params 要传递给callback的参数
     */
    Jsonp.send = function (url, callbackName, params) {
        Jsonp.appendScript(Jsonp.generateUrl(url, callbackName, params));
    };
    /**
     * @param {string} url
     */
    Jsonp.appendScript = function (url) {
        document.getElementsByTagName('head')[0].appendChild(Jsonp.createScript(url));
    };
    /**
     * 构造适用于jsonp的url
     * Jsonp.getUTC用于避免缓存
     * @param {string} url
     * @param {string} callbackName
     * @param {object} params 要传递给API接口的参数
     * @return {string} result
     */
    Jsonp.generateUrl = function (url, callbackName, params) {
        return encodeURI(url + '?callback=' + callbackName + String(Jsonp.getUTC()) + Jsonp.generateParam(params));
    };
    /**
     * @param {string} url
     * @return {HTMLElement} result
     */
    Jsonp.createScript = function (url) {
        var script = document.createElement('script');
        script.setAttribute('src', url);
        return script;
    };
    /**
     * @param {object} params
     * @return {string} result
     */
    Jsonp.generateParam = function (params) {
        var keys = Object.keys(params);
        if (keys.length === 0)
            return '';
        var param = '';
        keys.map(function (key, index) {
            if (index === 1) {
                param += '?';
            }
            if (index === (keys.length - 1)) {
                return param += key + '=' + JSON.stringify(params[key]);
            }
            param += key + '=' + String(params[key]) + '&';
        });
    };
    /**
     * 返回当前UTC时间
     * @return {number} result
     */
    Jsonp.getUTC = function () {
        return Date.UTC(currentUTC.getFullYear(), currentUTC.getMonth() + 1, currentUTC.getDate(), currentUTC.getMinutes(), currentUTC.getSeconds(), currentUTC.getMilliseconds());
    };
    return Jsonp;
}());
